﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.客户管理;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.ClientInfoCommands;
using SmartProduction.Read.API.Dtos;

namespace SmartProduction.Read.API.Application.CommandHandlers.ClientInfoHandler
{
    /// <summary>
    /// 客户信息查询命令处理
    /// </summary>
    public class ClientInfoQueryCommandHandler : IRequestHandler<ClientInfoQueryCommand, APIResult<APIPaing<ClientInfoDTO>>>
    {
        /// <summary>
        /// 客户信息仓储
        /// </summary>
        private readonly IBaseRepository<ClientInfo> clientRepository;
        /// <summary>
        /// 客户行业仓储
        /// </summary>
        private readonly IBaseRepository<Industry> industryRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="clientRepository"></param>
        /// <param name="mapper"></param>
        public ClientInfoQueryCommandHandler(IBaseRepository<ClientInfo> clientRepository,IBaseRepository<Industry> industryRepository,IMapper mapper)
        {
            this.clientRepository = clientRepository;
            this.industryRepository = industryRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<APIPaing<ClientInfoDTO>>> Handle(ClientInfoQueryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<APIPaing<ClientInfoDTO>> result = new APIResult<APIPaing<ClientInfoDTO>>();
                result.Code = APIEnums.查询成功;
                result.Msg = APIEnums.查询成功.ToString();
                var linq = from a in industryRepository.GetAll().Result
                           join b in clientRepository.GetAll().Result on a.IndustryId equals b.IndustryId
                           select new ClientInfoDTO
                           {
                               ClientInfoAddr = b.ClientInfoAddr,
                               ClientInfoName = b.ClientInfoName,
                               ClientInfoClass = b.ClientInfoClass,
                               ClientInfoEmail = b.ClientInfoEmail,
                               ClientInfoId = b.ClientInfoId,
                               ClientInfoLabel = b.ClientInfoLabel,
                               ClientInfoType = b.ClientInfoType,
                               ClientInfoNo = b.ClientInfoNo,
                               ClientInfoNumer = b.ClientInfoNumer,
                               ClientInfoPhone = b.ClientInfoPhone,
                               ClientInfoStatu = b.ClientInfoStatu,
                               CountryCity = b.CountryCity,
                               CountryName = b.CountryName,
                               FirmLabel = b.FirmLabel,
                               IndustryId = b.IndustryId,
                               IndustryName = a.IndustryName,
                           };
                var count = linq.Count();
                var page = (int)Math.Ceiling(count * 1.0 / request.PageSize);
                var models = linq.OrderByDescending(x => x.ClientInfoId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
                APIPaing<ClientInfoDTO> paging = new APIPaing<ClientInfoDTO>();
                paging.PageCount = page;
                paging.TotalCount = count;
                paging.Items = mapper.Map<List<ClientInfoDTO>>(models);
                result.data = paging;
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
